package com.xuhao.didi.oksocket.adapter;

import com.xuhao.didi.oksocket.ResourceTable;
import com.xuhao.didi.oksocket.data.LogBean;
import ohos.agp.components.*;
import ohos.app.Context;
import java.util.ArrayList;
import java.util.List;

public class LogAdapter extends BaseItemProvider {
    private List<LogBean> mDataList = new ArrayList<>();
    private Context context;

    /**
     * 初始化
     *
     * @param context
     */
    public LogAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mDataList != null && position >= 0 && position < mDataList.size()) {
            return mDataList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_log_item, null, false);
        } else {
            cpt = convertComponent;
        }
        LogBean bean = mDataList.get(position);
        Text mLog = (Text) cpt.findComponentById(ResourceTable.Id_logtext);
        Text mTime = (Text) cpt.findComponentById(ResourceTable.Id_time);
        mLog.setText(bean.mLog);
        mTime.setText(bean.mTime);
        return cpt;
    }

    public List<LogBean> getDataList() {
        return mDataList;
    }
}
